﻿package com.unifiedfield{
	import flash.display.MovieClip;
	import flash.geom.ColorTransform;
	import flash.events.Event;
	public class threedspace {
		public var space:MovieClip;
		private var focalLength = 1200;
		private var screenPoints:Array;
		private var pointsArray:Array;
		private var allPills:Array;
		private var ColorPills:Array;
		private var COLORS:Array;
		private var NAMES:Array;
		private var cubeAxisRotations:Array;
		public function threedspace(spacemc,n) {
			space=null;
			space=spacemc;
			space.x=1920/2;
			space.y=1200/2;
			COLORS=new Array("0xE19D81","0xE8080D","0x470000","0xFFFF0F","0xFFC800","0xFF7E00","0xD2FF0A","0x6FF517","0x26B300","0x92FAFF","0x30C7B1","0x005D1C","0x32BEFF","0x1E81F0","0x0030AE","0xFA60DE","0x8C1ECA","0x300392");
			NAMES=new Array();
			cubeAxisRotations=new Array(2);
			screenPoints=new Array(new Object(),new Object());
			pointsArray=new Array(new Array(),new Array());
			allPills=new Array();
			ColorPills=new Array();
			for (var i=0; i<COLORS.length; i++) {
				ColorPills[i]=new Array();
			}
			cubeAxisRotations[0] = make3DPoint(0,0,0);
			cubeAxisRotations[1] = make3DPoint(0,0,0);
			space.addEventListener(Event.ENTER_FRAME,rotateCube);
			for(i=0;i<n.length;i++){
				addNode(n[i],0);
			}
		}
		public function addNode(nam,shad){
			NAMES.push(nam);
			var min=100;
			var tot=300;
			var toadd = new pill();
			toadd.namu.text=nam;
			if(shad==0||shad==undefined||shad==null){
				toadd.shade=COLORS[Math.floor(Math.random()*COLORS.length)];
			}else{
				toadd.shade=shad;
			}
			if(pointsArray[0].length>pointsArray[1].length){
				pointsArray[1].push(make3DPoint(posorneg()*(Math.random()*tot+min),posorneg()*(Math.random()*tot+min),posorneg()*(Math.random()*tot+min)));
				toadd.name="1P"+(pointsArray[1].length-1);
			}else{
				pointsArray[0].push(make3DPoint(posorneg()*(Math.random()*tot+min),posorneg()*(Math.random()*tot+min),posorneg()*(Math.random()*tot+min)));
				toadd.name="0P"+(pointsArray[0].length-1);
			}
			updateColor(toadd.shade,toadd);
			toadd.scaleX=toadd.scaleY=1+(space.numChildren*0.02);
			space.addChild(toadd);
			allPills.push(toadd);
			for(var i=0;i<COLORS.length;i++){
				if(toadd.shade==COLORS[i]){
					ColorPills[i].push(toadd);
				}
			}
		}
		public function clearweb(){
			NAMES=new Array();
			screenPoints=new Array(new Object(),new Object());
			pointsArray=new Array(new Array(),new Array());
			allPills=new Array();
			ColorPills=new Array();
			for (var i=0; i<COLORS.length; i++) {
				ColorPills[i]=new Array();
			}
			while(space.numChildren>0){
				space.removeChildAt(0);
			}
		}
		public function pauseweb(t){
			if(t){
				space.removeEventListener(Event.ENTER_FRAME,rotateCube);
			}else{
				space.addEventListener(Event.ENTER_FRAME,rotateCube);
			}
		}
		private function make3DPoint(tx,ty,tz) {
			var point = new Object();
			point.fx = tx;
			point.fy = ty;
			point.fz = tz;
			return point;
		}
		private function make2DPoint(tx,ty, depth, scaleFactor) {
			var point = new Object();
			point.fx = tx;
			point.fy = ty;
			point.depth = depth;
			point.scaleFactor = scaleFactor;
			return point;
		}
		private function Transform3DPointsTo2DPoints(points, axisRotations) {
			var sx = Math.sin(axisRotations.fx);
			var cx = Math.cos(axisRotations.fx);
			var sy = Math.sin(axisRotations.fy);
			var cy = Math.cos(axisRotations.fy);
			var sz = Math.sin(axisRotations.fz);
			var cz = Math.cos(axisRotations.fz);
			var tx,ty,tz, xy,xz, yx,yz, zx,zy, scaleFactor;
			var i = points.length;
			var TransformedPointsArray = new Array(i);
			while (i--) {
				tx = points[i].fx;
				ty = points[i].fy;
				tz = points[i].fz;
				// rotation around x
				xy = cx*ty - sx*tz;
				xz = sx*ty + cx*tz;
				// rotation around y
				yz = cy*xz - sy*tx;
				yx = sy*xz + cy*tx;
				// rotation around z
				zx = cz*yx - sz*xy;
				zy = sz*yx + cz*xy;
				scaleFactor = focalLength/(focalLength + yz);
				tx = zx*scaleFactor;
				ty = zy*scaleFactor;
				tz = yz;
				var tad:Object = make2DPoint(tx, ty, -tz, scaleFactor);
				TransformedPointsArray[i] = tad;
			}
			return TransformedPointsArray;
		}
		private function posorneg():Number {
			if (Math.round(Math.random())) {
				return -1;
			}
			return 1;
		}
		private function rotateCube(ev:Event) {
			//cubeAxisRotations[0].fx += .005;
			cubeAxisRotations[0].fz += .005;
			//cubeAxisRotations[0].fz += .1;
			screenPoints[0]=Transform3DPointsTo2DPoints(pointsArray[0], cubeAxisRotations[0]);
			//cubeAxisRotations[1].fx -= .005;
			cubeAxisRotations[1].fz -= .005;
			//cubeAxisRotations[1].fz -= .005;
			screenPoints[1]=Transform3DPointsTo2DPoints(pointsArray[1], cubeAxisRotations[1]);
			allPills.sortOn("scaleX");
			ev.currentTarget.graphics.clear();
			for (var i=0; i<allPills.length; i++) {
				ev.currentTarget.setChildIndex(allPills[i],i);
				pillAnim(allPills[i]);
			}
			for (var h=0; h<ColorPills.length; h++) {
				ev.currentTarget.graphics.lineStyle(1,COLORS[h],.5);
				for (i=0; i<ColorPills[h].length; i++) {
					ev.currentTarget.graphics.moveTo(ColorPills[h][0].x,ColorPills[h][0].y);
					ev.currentTarget.graphics.lineTo(ColorPills[h][i].x,ColorPills[h][i].y);
				}
			}
		}
		private function pillAnim(me) {
			var at=Number(me.name.substr(0,1));
			var num=Number(me.name.substr(2,2));
			var size=Math.abs(screenPoints[at][num].scaleFactor/2);
			me.x = screenPoints[at][num].fx;
			me.y = screenPoints[at][num].fy;
			me.scaleX = me.scaleY = size;
			me.alpha = me.scaleX;
		}
		private function updateColor(colo,pill) {
			var colorTransform:ColorTransform=pill.body.base.transform.colorTransform;
			colorTransform.color=colo;
			pill.body.base.transform.colorTransform = colorTransform;
		}
	}
}